package com.macro.mall.service.impl;

import com.github.pagehelper.PageHelper;
import com.macro.mall.dao.SmsFlashPromotionProductRelationDao;
import com.macro.mall.dto.SmsFlashPromotionProduct;
import com.macro.mall.mapper.SmsFlashPromotionProductRelationMapper;
import com.macro.mall.model.SmsFlashPromotionProductRelation;
import com.macro.mall.model.SmsFlashPromotionProductRelationExample;
import com.macro.mall.service.SmsFlashPromotionProductRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 限时购商品关联管理Service实现类
 * Created by macro on 2018/11/16.
 */
@Service
public class SmsFlashPromotionProductRelationServiceImpl implements SmsFlashPromotionProductRelationService {
    @Autowired
    private SmsFlashPromotionProductRelationMapper relationMapper;
    @Autowired
    private SmsFlashPromotionProductRelationDao relationDao;
    @Override
    public int create(List<SmsFlashPromotionProductRelation> relationList) {
        for (SmsFlashPromotionProductRelation relation : relationList) {
            relationMapper.insert(relation);
        }
        return relationList.size();
    }

    @Override
    public int update(Long id, SmsFlashPromotionProductRelation relation) {
        relation.setId(id);
        return relationMapper.updateByPrimaryKey(relation);
//        //根据1d查询sms_flash_promotion_product_relation得列活动信息及场次信息
//        SmsFlashPromotionProductAndsession smsflashPromotionProductAndsession = relationMapper =.selectRelationAndsession(id);
////校验当前场次是否已开始
//        Date startTime = smsFlashPromotionProductAndSession.getstartTime();
//        Date endTime = smsFlashPromotionProductAndSession.getEndTime();
//        Date now = new Date();
//        if (now.before(startTime) || now.after(endTime)) {
//            throw new RuntimeException("当前场次未开始，不能修改");
////获取当前时间
////根据date判断当前日期是否在抢购活动的日期范内
//            Date startDate = smsFlashPromotionProductAndsession.getStartDate();
//            Date endDate = smsFlashPromotionProductAndSession.getEndDate();
//            if (now.before(startDate) || now.after(endDate)) {
//                throw new RuntimeException("当前日期不在抢购活动的日期范围内，不能修改");
////根据relat1on的sku1d及第一步得到的活动1d及场次1d，作为组合条件，刚除sms_flash_promotion_sku_reation表
//                Long skuId = relation.getskuId();
//                Long flashPromotionId = smsFlashPromotionProductAndSession.getFlashPromotionId();
//                Long flashPromotionSessionId = smsflashPromotionProductAndSession.getFlashPromotionsessionId();
////组装信息，批量保存至sms_flash_promotion_sku_relation表中
////更新red1s的预热数据
//                return relationMapper.updateByPrimaryKey(relation);
//            }
//        }
//        return 0;
    }

    @Override
    public int delete(Long id) {
        return relationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public SmsFlashPromotionProductRelation getItem(Long id) {
        return relationMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SmsFlashPromotionProduct> list(Long flashPromotionId, Long flashPromotionSessionId, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        return relationDao.getList(flashPromotionId,flashPromotionSessionId);
    }

    @Override
    public long getCount(Long flashPromotionId, Long flashPromotionSessionId) {
        SmsFlashPromotionProductRelationExample example = new SmsFlashPromotionProductRelationExample();
        example.createCriteria()
                .andFlashPromotionIdEqualTo(flashPromotionId)
                .andFlashPromotionSessionIdEqualTo(flashPromotionSessionId);
        return relationMapper.countByExample(example);
    }
}
